﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace OnlineCourse.API.Helper
{
    public static class EnumHelper
    {
        public static string GetEnumDescription<TEnum>(this Enum @enum)
        {
            Type enumType = typeof(TEnum);

            if (!enumType.IsEnum)
            {
                throw new ArgumentException("必须使用枚举类型");
            }
            var name = Enum.GetName(enumType, Convert.ToInt32(@enum));
            if (name == null)
            {
                return string.Empty;
            }

            object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (objs == null || objs.Length == 0)
            {
                return string.Empty;
            }
            else
            {
                DescriptionAttribute attr = objs[0] as DescriptionAttribute;
                return attr.Description;
            }
        }
    }
}
